$(document).ready(function () {
    // 初始化区域联动下拉框
    $("[name='areaDiv']").initAreaSelect();

    doBranchBank();
});

// 获取支行信息
function getBranchBank() {
    var cardProvince = $("#card\\.province").val(),
        cardCity = $("#card\\.city").val(),
        cardDistrict = $("#card\\.district").val(),
        cardNum = $("#card\\.cardNo").val();
    branchBankId = $('#card\\.branchBankId').attr('areaCode');


    $("#card\\.bankName").val('');
    $("#card\\.branchBankId").html('<option value="">请选择</option>');

    if (!cardProvince || !cardCity || !cardDistrict) {
        return;
    }
    if (checkBankCardLuhn(cardNum) != true) {
        return;
    }
    $.ajax({
        type: 'POST',
        url: baseURL + "bank/selectByProviceAndCity",
        data: {"provinceCode": cardProvince, "cityCode": cardCity, "countyCode": cardDistrict, "bankCardNo": cardNum},
        dataType: 'json',
        success: function (data) {
            $("#card\\.branchBankId").show();
            $("#card\\.branchBankName").prop('type', 'hidden');
            if (data && data.length > 0) {
                $('#card\\.bankName').val(data[0].bankName);
                var html_ = '<option value="">请选择</option>';
                $.each(data, function (i, item) {

                    //支行信息回显
                    if (branchBankId == item.id) {
                        html_ += '<option selected="selected" value=' + item.id + '>' + item.subbranchBank + '</option>';
                    }
                    html_ += '<option value=' + item.id + '>' + item.subbranchBank + '</option>';
                });
                $("#card\\.branchBankId").html(html_);
                $("#card\\.branchBankName").val('');
            } else {
                $("#card\\.branchBankId").hide();
                $("#card\\.branchBankId").val('');
                $("#card\\.branchBankName").prop('type', 'text');
            }
        }
    });
}

//处理支行信息
function doBranchBank() {
    $("#card\\.province").change(getBranchBank);
    $("#card\\.city").change(getBranchBank);
    $("#card\\.district").change(getBranchBank);
    $("#card\\.cardNo").change(getBranchBank);
    $("#card\\.bankName").click(getBranchBank);
}

function checkBankCardNo(type) {
    // 初始化区域联动下拉框
    $("#openAddress").initAreaSelect();
    $("#card\\.cardNo").val('');
    $("#card\\.bankName").val('');
    $("#card\\.branchBankId").val('');
    $("#card\\.branchBankId").html('');
    $("#card\\.branchBankName").val('');
    if (1 == type || '' == type) {
        $("#card\\.cardNo").addClass("checkBankCard");
        $("#card\\.branchBankId").show();
        $("#card\\.branchBankName").prop('type', 'hidden');
    }
    if (2 == type) {
        $("#card\\.cardNo").removeClass("checkBankCard");
        $("#card\\.branchBankId").hide();
        $("#card\\.branchBankName").prop('type', 'text');
    }
}


$(function () {
    $("[name='areaDiv']").initAreaSelect();

    function alarmFormatter(value, options, row) {
        return '<img src=' + value + ' style="width:50px;height:50px;" class="img-polaroid" />';
    };

    $("#jqGrid").jqGrid({
        url: baseURL + 'store/list',
        datatype: "json",
        colModel: [
            {label: 'id', name: 'id', width: 30, key: true},
            {label: '门店名称', name: 'name', width: 40},
            {
                label: '头像',
                name: 'url',
                width: 20,
                align: "center",
                sortable: false,
                editable: true,
                formatter: alarmFormatter
            },
            {label: '企业名称', name: 'merchantName', index: "merchant_name", width: 40},
            {label: '法人', name: 'legalperson', index: "legalPerson", width: 15},
            {label: '门店地址', name: 'address', idenx: 'address', width: 45},
            {label: '联系电话', name: 'mobilePhone', index: "mobile_phone", width: 35},
            {
                label: '审核状态',
                name: 'auditStatus',
                index: "audit_status",
                width: 20,
                formatter: function (value, options, row) {
                    if (value == 0) {
                        return '<span class="label label-danger">未审核</span>';
                    } else if (value == 1) {
                        return '<span class="label label-info">审核中</span>';
                    } else if (value == 2) {
                        return '<span class="label label-success">审核通过</span>';
                    } else if (value == 4) {
                        return '<span class="label label-warning">审核不通过</span>';
                    } else {
                        return "";
                    }
                }
            },
            {
                label: '购买状态',
                name: 'softwareStatus',
                index: "software_status",
                width: 20,
                formatter: function (value, options, row) {
                    if (value == 3) {
                        return '<span class="label label-success">已购买</span>';
                    } else if (value == 1) {
                        return '<span class="label label-info">试用</span>';
                    } else if (value == 2) {
                        return '<span class="label label-danger">试用过期</span>';
                    } else if (value == 4) {
                        return '<span class="label label-warning">购买过期</span>';
                    } else {
                        return "";
                    }
                }
            },
            {label: '有效期', name: 'expiryDate', index: "expiry_date", width: 40},
            {label: '创建时间', name: 'insertDate', index: "insert_date", width: 40},
        ],
        viewrecords: true,
        height: 720,
        rowNum: 10,
        rowList: [10, 30, 50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth: true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader: {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames: {
            page: "page",
            rows: "limit",
            order: "order"
        },
        gridComplete: function () {
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
        },
        ondblClickRow: function (id) {
            vm.id = id;
            vm.detal();
        }

    });

});

//暂时没找到点击图片关闭弹出层
function imgShow(obj) {
    var img = $(obj).find("img")[0].src;
    // alert(img);
    $("#showimg").attr('src', img);
    layer.open({
        type: 1,
        title: false,
        closeBtn: 0,
        area: '800px',
        //  time: 3000,//3秒自动关闭
        skin: 'layui-layer-nobg', //没有背景色
        shadeClose: true,
        content: $("#imgId").html()
    });
}

var vm = new Vue({
    el: '#dcapp',
    data: {
        q: {
            name: null,
        },
        id: null,
        showList: true,
        title: null,
        store: {},
        bankCard: {},
        options: [],
        logoImgList:'[]',
        logoImg:null,
    },
    created: function () {

    },
    methods: {
        query: function () {
            vm.reload();
        },
        update: function () {
            var storeId = getSelectedRow();//选择一条
            if (storeId == null) {
                return;
            }
            vm.getKind();
            vm.getStore(storeId);
            vm.logoImgList = vm.store.logoImg;
            vm.logoImg = vm.logoImgList;
            vm.initImg();
            vm.showList = false;
            vm.showDetal = false;
            vm.title = "编辑门店";
        },
        add: function () {
            vm.logoImgList = '[]';
            vm.logoImg = eval('(' + vm.logoImgList + ')');
            vm.initImg();
            vm.getKind();
            vm.store = {};
            vm.showList = false;
            vm.title = "增加门店";

            var	form = $(".form-horizontal");
        },
        detal: function () {
            vm.getKind();
            vm.getStore(vm.id);
            vm.showList = false;
            vm.title = "详情";
        },
        saveOrUpdate: function () {

            //门店信息
            vm.store.province =  $("#cardProvince").val();
            vm.store.city =  $("#cardCity").val();
            vm.store.district =  $("#cardDistrict").val();

            vm.store.logoImg = vm.logoImg;

            var url = vm.store.id == null ? "store/saveStore" : "store/updateStore";
            var formJson = JSON.stringify(vm.store);
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: formJson,
                success: function (r) {
                    if (r.code === 0) {
                        alert('操作成功', function () {
                            vm.reload();
                        });
                    } else {
                        alert(r.msg);
                    }
                }
            });
            // parent.location.reload();
        },
        del: function () {
            var storeIds = getSelectedRows();
            if (storeIds == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                $.ajax({
                    type: "POST",
                    url: baseURL + "store/delete",
                    contentType: "application/json",
                    data: JSON.stringify(storeIds),
                    success: function (r) {
                        if (r.code === 0) {
                            alert('操作成功', function () {
                                vm.reload();
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        getStore: function (storeId) {
            $.ajax({
                type: "GET",
                url: baseURL + "store/info/" + storeId,
                async: false,
                success: function (r) {
                    vm.store = r.store;
                }
            });

        },
        getKind: function () {
            $.get(baseURL + "common/getKind", function (r) {
                vm.options = r.result;
            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {'key': vm.q.key},
                page: page
            }).trigger("reloadGrid");
        },
        initImg: function () {
            $("#upload_logo").initWebUploader(vm.logoImg, "logoImg", 1, vm.store.id, vm.logoImg.length);
        },
    }
});